package com.qingmeng.portal.controller;

import com.qingmeng.common.api.CommonResult;
import com.qingmeng.common.utils.FileUtil;
import com.qingmeng.portal.enums.LoginResult;
import com.qingmeng.portal.enums.RegisterResult;
import com.qingmeng.portal.model.vo.UserPersonalInformationVo;
import com.qingmeng.portal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "UserController", description = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public CommonResult<Integer> register(
            @ApiParam("用户名")@RequestParam("username") String username,
            @ApiParam("密码")@RequestParam("password") String password
    ) {
        RegisterResult register = userService.register(username, password);
        if (register == RegisterResult.REG_SUCCESS) {
            return CommonResult.success(1,register.getMessage());
        }
        return CommonResult.failed(register.getMessage());
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CommonResult<Integer> login(
            @ApiParam("用户名") @RequestParam("usernaem") String username,
            @ApiParam("密码") @RequestParam("passworrd") String password) {
        LoginResult login = userService.login(username, password);
        if (login ==  LoginResult.LOGIN_SUCCESS) {
            return CommonResult.success(1, login.getMessage());
        }
        return CommonResult.failed(login.getMessage());
    }
    @ApiOperation("用户上传图片")
    @PostMapping("/uploadPhoto")
    public CommonResult<String> uploadPhoto(@ApiParam("上传的图片")@RequestParam("photo")MultipartFile file,
                                            @ApiParam("图片类型 1表示用户头像，2表示blog相关图片")@RequestParam("tag")int tag) throws Exception {
        String filePath = FileUtil.portalUploadPhoto(file, tag);
        if (filePath != null) {
            return CommonResult.success(filePath,"上传图片成功");
        }
        return CommonResult.failed("上传图片失败，请重试");
    }

    @ApiOperation("根据id查询用户信息")
    @GetMapping("/getUserById")
    public CommonResult<UserPersonalInformationVo> getUserById(@ApiParam("用户id")@RequestParam("id")Long id) {
        UserPersonalInformationVo userInformation = userService.getUserById(id);
        if (userInformation != null) {
            return CommonResult.success(userInformation,"查询成功");
        }
        return CommonResult.failed("该用户不存在");
    }

    @ApiOperation("根据用户名查询用户信息")
    @GetMapping("/getUserByUsername")
    public CommonResult<UserPersonalInformationVo> getUserByUsername(@ApiParam("用户名")@RequestParam("username")String username) {
        UserPersonalInformationVo userInformation = userService.getUserByUserName(username);
        if (userInformation != null) {
            return CommonResult.success(userInformation,"查询成功");
        }
        return CommonResult.failed("该用户不存在");
    }
}
